package com.itheima.prize.api.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: prize
 * @description: 调用lua脚本, 实现redis的原子操作
 * @author: zhanghz001
 * @create: 2021-06-15 20:16
 **/
// @Component
public class ZhzLuaScript {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DefaultRedisScript<Long> script;
    
    @PostConstruct
    public void init() {
        script = new DefaultRedisScript<>();
        script.setResultType(Long.class);
        script.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/tokenCheck.lua")));
    }
    
    public Long tokenCheck(String gameKey, String currenttime) {
        List<String> keys = new ArrayList<>();
        keys.add(gameKey);
        keys.add(currenttime);
        
        Long result = (Long) redisTemplate.execute(script, keys, 0, 0);
        return result;
    }
    
}
